package xyz.syyrjx.blog.controller.tool.ticTacToe;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.syyrjx.blog.entity.Result;
import xyz.syyrjx.blog.entity.ticTacToe.Gamer;
import xyz.syyrjx.blog.entity.ticTacToe.Room;
import xyz.syyrjx.blog.exception.TicTacToeException;
import xyz.syyrjx.blog.util.ConstValuePool;
import xyz.syyrjx.blog.util.RandomUtil;
import xyz.syyrjx.blog.websocket.TicTacToeWebSocket;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/4/25 18:58
 * @description ...
 * @since v0.0.1
 */
@Slf4j
@Controller
@RequestMapping("/tools/ticTacToe")
public class TicTacToeController {

    @Resource
    private TicTacToeWebSocket ticTacToeWebSocket;

    @Value("${tic.tac.toe.socket.url}")
    private String socketUrl;

    @RequestMapping("/bigSmall")
    public String bigSmall() {
        return "tools/ticTacToe/bigSmall";
    }

    @RequestMapping("/endless")
    public String endless() {
        return "tools/ticTacToe/endless";
    }

    @ResponseBody
    @RequestMapping("/createRoom")
    public Result createRoom(int roomType) {
        Gamer gamer = new Gamer();
        gamer.setId("tic_tac_toe_gamer" + UUID.randomUUID());
        Room room = new Room();
        room.setId("tic_tac_toe_room" + UUID.randomUUID());
        room.getGamers()[0] = gamer;
        room.setType(roomType);
        room.setBoard(new int[3][3]);
        Room.createTicTacToeRoom(room.getId(),room);
        return Result.success(room);
    }

    @ResponseBody
    @RequestMapping("/getSocketUrl")
    public Result getSocketUrl() {
        return Result.success(socketUrl);
    }

    @ResponseBody
    @RequestMapping("/joinRoom")
    public Result joinRoom(String roomId) {
        Gamer gamer = new Gamer();
        gamer.setId("tic_tac_toe_gamer" + UUID.randomUUID());
        Room room = Room.getTicTacToeRoom(roomId);
        room.getGamers()[1] = gamer;
        return Result.success(room);
    }

    @ResponseBody
    @RequestMapping("/doIt")
    public Result doIt(String roomId, int site, String id, int size) throws JsonProcessingException {


        Room room = Room.getTicTacToeRoom(roomId);
        Gamer runningGamer = room.getGamer(id);
        if (runningGamer == null) {
            throw new TicTacToeException("玩家不存在");
        }
        if (runningGamer.getType() != 1) {
            throw new TicTacToeException("不是你的回合");
        };

        // 下棋
        int x = site / 10;
        int y = site % 10;
        int data = room.getBoard()[x][y];
        if ((size == 1 && data != 0) || (size == 10 && data / 10 != 0)) {
            throw new TicTacToeException("该位置已存在棋子");
        }
        room.getBoard()[x][y] += runningGamer.getPower() * size;
        if (size == 10) {
            room.removeLastBig(runningGamer);
            runningGamer.addBig(x,y);
        }

        // 行动交换
        runningGamer.setType(0);
        Gamer other = room.getOther(runningGamer.getId());
        other.setType(1);
        room.judge();
        log.debug("{}",room.getState());

        String res = ConstValuePool.OBJECT_MAPPER.writeValueAsString(Result.create(2,room,null));
        ticTacToeWebSocket.sendMessage(res, runningGamer.getId());
        ticTacToeWebSocket.sendMessage(res, other.getId());

        return Result.success(null);
    }


    @ResponseBody
    @RequestMapping("/ready")
    public Result ready(String roomId,String id) throws JsonProcessingException {

        Room room = Room.getTicTacToeRoom(roomId);
        Gamer gamer = room.getGamer(id);
        if (gamer.getState() == 1) {
            throw new TicTacToeException("你已经准备过了");
        }
        if (room.getState() > 0) {
            room.setState(-2);
        }

        int oldState = room.getState();
        if (gamer.getState() == 0) {
            gamer.setState(1);
            room.setState(room.getState() + 1);
        }
        int newState = room.getState();

        // 分配
        if (oldState < 0 && newState == 0) {
            log.debug("开始");
            Gamer g = room.getGamers()[RandomUtil.randomInt(2)];
            g.setType(1);
            g.setPower(1);
            Gamer o = room.getOther(g.getId());
            o.setPower(2);
            room.init();
        }

        String res = ConstValuePool.OBJECT_MAPPER.writeValueAsString(Result.create(2,room,null));
        ticTacToeWebSocket.sendMessage(res, gamer.getId());
        Gamer other = room.getOther(id);
        if (other != null) {
            ticTacToeWebSocket.sendMessage(res, other.getId());
        }
        return Result.success(null);
    }

    @ResponseBody
    @RequestMapping("submitDiague")
    public Result submitDiague(String roomId, String id, String diague) throws JsonProcessingException {
        Room room = Room.getTicTacToeRoom(roomId);
        Gamer other = room.getOther(id);
        String res = ConstValuePool.OBJECT_MAPPER.writeValueAsString(Result.create(3, diague, null));
        ticTacToeWebSocket.sendMessage(res, other.getId());
        return Result.success(null);
    }

}
